基础知识_设计模式

文章目录
  1. 1. 单例模式
    1. 1.1. 懒汉式
    2. 1.2. 饿汉式
  2. 2. Oberver观察者模式

记录一下常见的设计模式的实现方法(Cpp实现)。

单例模式

当只允许类创建一个实例的时候,可以使用单例模式。

懒汉式

  • 懒汉式是在需要创建实例的时候才创建。
  • 将构造函数设置为私有可以组织创建对象,然后通过static函数从内部调用构造函数。

线程不安全

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
class Singleton{
public:
static Singleton * getInstance(){
if(pinstance==nullptr){
pinstance=new Singleton;
}
return pinstance;
}

private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton &s);
const Singleton &operator=(const Singleton &s);
private:
static Singleton *pinstance;

};

Singleton * Singleton::pinstance=nullptr;

int main(){
Singleton *s1=Singleton::getInstance();
Singleton *s2=Singleton::getInstance();
if(s1==s2){
cout<<"s1==s2"<<endl;
}
return 0;
}

线程安全

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <mutex>
using namespace std;
class Singleton{
public:
static Singleton * getInstance(){
if(pinstance==nullptr){
std::lock_guard<std::mutex> lock(mutex1);
if(pinstance==nullptr){
pinstance=new Singleton;
}
}
return pinstance;
}

private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton &s);
const Singleton &operator=(const Singleton &s);
private:
static Singleton *pinstance;
static std::mutex mutex1;
};

Singleton * Singleton::pinstance=nullptr;
std::mutex Singleton::mutex1;

int main(){
Singleton *s1=Singleton::getInstance();
Singleton *s2=Singleton::getInstance();
if(s1==s2){
cout<<"s1==s2"<<endl;
}
return 0;
}

effective c++中的实现

1
2


饿汉式

  • 饿汉式是程序运行的时候马上就创建。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    //本身就线程安全
    #include <iostream>
    using namespace std;
    class Singleton{
    public:
    static Singleton * getInstance(){
    return pinstance;
    }

    private:
    Singleton(){}
    ~Singleton(){}
    Singleton(const Singleton &s);
    const Singleton &operator=(const Singleton &s);
    private:
    static Singleton *pinstance;

    };

    Singleton * Singleton::pinstance=new (std::nothrow)Singleton;

    int main(){
    Singleton *s1=Singleton::getInstance();
    Singleton *s2=Singleton::getInstance();
    if(s1==s2){
    cout<<"s1==s2"<<endl;
    }
    return 0;
    }

Oberver观察者模式

观察者模式又叫发布订阅模式,在被观察者发生改变时,多个观察者应当能发现其变化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;

class Observer;

//被观察者
class Subject{
public:
Subject(int a):state(a){}
void addObserver(Observer *obs){
observers.push_back(obs);
}
void delObserver(Observer *obs){
observers.remove(obs);
}
int getState(){
return state;
}
void setState(int _state){
state=_state;
}
void notifyAll();
private:
int state;
list<Observer*> observers;
};

//观察者-虚基类
class Observer{
public:
Observer(Subject *sub):_subject(sub){}
virtual void update()=0;
protected:
Subject *_subject;
};

//观察者1
class Observer1:public Observer{
public:
Observer1(Subject *sub):Observer(sub){}
void update() override{
cout<<"Observer1: "<<_subject->getState()<<endl;
}
};

//观察者2
class Observer2:public Observer{
public:
Observer2(Subject *sub):Observer(sub){}
void update() override{
cout<<"Observer2: "<<_subject->getState()<<endl;
}
};

/* 注意Observer类进行了前向声明,但是不能访问其类内成员
* 在Oberver类的定义之后,a才能访问其update()方法.
*/
void Subject::notifyAll(){
for(auto a:observers){
a->update();
}
}

int main(){
Subject *sub1=new Subject(10);
Observer1 *obs1=new Observer1(sub1);
Observer2 *obs2=new Observer2(sub1);

sub1->addObserver(obs1);
sub1->addObserver(obs2);

cout<<"init:state="<<sub1->getState()<<endl;
cout<<"set:state=20"<<endl;

sub1->setState(20);

sub1->notifyAll();

delete sub1;
delete obs1;
delete obs2;

return 0;
}